package com.company.project.controller.store;

import com.alibaba.fastjson.JSONObject;
import com.company.project.vo.req.store.QueryReqVO;
import com.company.project.vo.req.store.StoreRegisterReqVO;
import com.company.project.vo.resp.store.RegisterRespVO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.util.List;

import com.company.project.common.utils.DataResult;

import com.company.project.entity.SysStoreEntity;
import com.company.project.service.SysStoreService;

import javax.validation.Valid;


/**
 * @author YangYulin
 * @email YangYulinit@163.com
 * @date 2020-07-19 19:57:33
 */
@Controller
@RequestMapping("/")
@Api(tags = "门店管理-门店信息")
@Slf4j
public class SysStoreController {
    @Autowired
    private SysStoreService sysStoreService;

    /**
     * 跳转到页面
     */
    @GetMapping("/index/sysStore")
    public String sysStore() {
        return "sysstore/list";
    }

    @GetMapping("/index/sysStoreCheck")
    public String sysStoreCheck() {
        return "sysstore/checkList";
    }


    @ApiOperation(value = "删除")
    @DeleteMapping("sysStore/delete")
    @RequiresPermissions("sysStore:delete")
    @ResponseBody
    public DataResult delete(@RequestBody @ApiParam(value = "id集合") List<String> ids) {
        sysStoreService.removeByIds(ids);
        return DataResult.success();
    }


    @ApiOperation(value = "更新")
    @PutMapping("sysStore/update")
    @RequiresPermissions(value = {"sysStore:update","sysStore:checkUpdate"},logical = Logical.OR)
    @ResponseBody
    public DataResult update(@RequestBody SysStoreEntity sysStore) {
        sysStoreService.updateById(sysStore);
        return DataResult.success();
    }

    @ApiOperation(value = "查询分页数据")
    @PostMapping("sysStore/listByPage")
    @RequiresPermissions("sysStore:list")
    @ResponseBody
    public DataResult<IPage<SysStoreEntity>> findListByPage(@RequestBody QueryReqVO sysStore) {
        Page page = new Page(sysStore.getPage(), sysStore.getLimit());
        QueryWrapper queryWrapper = new QueryWrapper();
        //查询条件示例
        //queryWrapper.eq("id", sysStore.getId());
        //  门店状态（1.正常2.停用 3.审核失败 4.审核中）

        //查询条件示例
        if (!StringUtils.isEmpty(sysStore.getStoreName())) {
            queryWrapper.like("store_name", sysStore.getStoreName());
        }
        if (!StringUtils.isEmpty(sysStore.getStoreNames())) {
            queryWrapper.or();
            queryWrapper.like("store_names", sysStore.getStoreNames());
        }

        if (!StringUtils.isEmpty(sysStore.getStatus())) {
            queryWrapper.in("status", String.valueOf(sysStore.getStatus()).split(","));
        } else {
            queryWrapper.eq("status", "1");
        }

        IPage<SysStoreEntity> iPage = sysStoreService.page(page, queryWrapper);
        iPage = sysStoreService.storeArea(iPage);
        log.info(JSONObject.toJSONString(iPage));
        return DataResult.success(iPage);
    }
    @ApiOperation(value = "通过用户id 查询门店信息")
    @GetMapping("sysStore/userIdByInfo")
    @ResponseBody
    public DataResult<SysStoreEntity> userIdByInfo(@RequestParam(required = true)String userId){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id",userId);
        return DataResult.success(sysStoreService.getOne(queryWrapper));
    }

    @ApiOperation(value = "通过门店id 查询门店信息")
    @GetMapping("sysStore/idByInfo")
    @ResponseBody
    public DataResult<SysStoreEntity> idByInfo(@RequestParam(required = true)String storeId){
        return DataResult.success(sysStoreService.getById(storeId));
    }

    @ApiOperation(value = "添加门店")
    @PostMapping("sysStore/addStore")
    @Transactional(rollbackFor = Exception.class)
    @ResponseBody
    @RequiresPermissions("sysStore:addStore")
    public DataResult addStore(@Valid @RequestBody StoreRegisterReqVO vo) {
        return DataResult.success(sysStoreService.register(vo, 1));
    }

    @ApiOperation(value = "门店注册")
    @PostMapping("sysStore/register")
    @Transactional(rollbackFor = Exception.class)
    @ResponseBody
    @RequiresPermissions("sysStore:register")
    public DataResult<RegisterRespVO> register(@Valid @RequestBody StoreRegisterReqVO vo) {
        return DataResult.success(sysStoreService.register(vo));
    }
}
